package net.chasing.androidbaseconfig.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import net.chasing.androidbaseconfig.R;

public class DialogUtils {
    private DialogUtils() {
    }

    public static Dialog createDialog(Context context, String msg, String sureTxt,
                                      View.OnClickListener clickSureListener) {
        return createDialog(context, "提示", msg, "取消", 0,
                null, sureTxt, 0, clickSureListener);
    }

    public static Dialog createDialog(Context context, String msg,
                                      String sureTxt, View.OnClickListener clickSureListener,
                                      String cancelTxt, View.OnClickListener clickCancelListener) {
        return createDialog(context, "提示", msg, cancelTxt, 0,
                clickCancelListener, sureTxt, 0, clickSureListener);
    }

    public static Dialog createDialog(Context context, String title, String msg,
                                      String cancelTxt, int cancelTxtColor,
                                      View.OnClickListener clickCancelListener,
                                      String sureTxt, int sureTxtColor,
                                      View.OnClickListener clickSureListener) {
        @SuppressLint("InflateParams") View view = LayoutInflater.from(context).inflate(R.layout.dialog_normal, null);
        Dialog dialog = new Dialog(context, androidx.appcompat.R.style.Theme_AppCompat_Light_Dialog_Alert);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(android.R.color.transparent);
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = (int) (GenericTools.getScreenWidth((Activity) context) - context.getResources().getDimension(R.dimen.dialog_width_space));
            window.setAttributes(attributes);
        }
        initView(dialog, view, title, msg, cancelTxt, cancelTxtColor, clickCancelListener, 0, sureTxt, sureTxtColor, clickSureListener, 0);
        return dialog;
    }

    private static void initView(final Dialog dialog, View view, String title, String msg,
                                 String cancelTxt, int cancelTxtColor, final View.OnClickListener clickCancelListener, int bgCancelColor,
                                 String sureTxt, int sureTxtColor, final View.OnClickListener onClickListener, int bgSureColor) {
        TextView tvTitle = view.findViewById(R.id.title);
        tvTitle.setText(title);
        TextView tvMsg = view.findViewById(R.id.message);
        tvMsg.setText(msg);
        TextView btnCancel = view.findViewById(R.id.cancel);
        btnCancel.setText(cancelTxt);
        if (bgCancelColor != 0) {
            btnCancel.setBackgroundColor(bgCancelColor);
        }

        if (cancelTxtColor != 0)
            btnCancel.setTextColor(cancelTxtColor);
        btnCancel.setOnClickListener(v -> {
            if (clickCancelListener != null) clickCancelListener.onClick(v);
            dialog.dismiss();
        });
        view.findViewById(R.id.close).setOnClickListener(v -> dialog.dismiss());
        TextView btnSure = view.findViewById(R.id.sure);
        btnSure.setText(sureTxt);
        if (bgSureColor != 0) {
            btnSure.setBackgroundColor(bgSureColor);
        }
        if (sureTxtColor != 0)
            btnSure.setTextColor(sureTxtColor);
        btnSure.setOnClickListener(v -> {
            if (onClickListener != null) onClickListener.onClick(v);
            dialog.dismiss();
        });
    }
}
